#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> step(m);
  for (auto &[a, _] : step) cin >> a;
  for (auto &[_, b] : step) cin >> b;
  int limit = min(n, 1000);
  const i64 inf = LLONG_MAX / 2;
  vector<i64> dist(limit * 2 + 1, inf);
  using pii = pair<i64, int>;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.emplace(0, limit);
  dist[limit] = 0;
  while (!pq.empty()) {
    auto [d, u] = pq.top();
    pq.pop();
    for (auto &[a, b] : step) {
      for (int dir = -1; dir <= 1; dir += 2) {
        int v = u + a * dir;
        if (v < 0 || v > limit * 2) continue;
        if (dist[v] <= d + b) continue;
        dist[v] = d + b;
        pq.emplace(dist[v], v);
      }
    }
  }
  vector<i64> cost(n + 1);
  for (int i = limit + 1; i <= limit * 2; i ++) {
    if (dist[i] == inf) {
      cout << "-1\n";
      return;
    }
    cost[i - limit] = dist[i];
  }
  vector<i64> dp(n + 1, inf);
  dp[0] = dp[1] = 0;
  for (int len = 2; len <= n; len ++) {
    for (int s = 1; s <= min(len - 1, limit); s ++) {
      dp[len] = min(dp[len], max(dp[s], dp[len - s]) + cost[s]);
    }
  }
  cout << dp[n] << endl;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T = 1, c;
  cin >> c >> T;
  for (int cas = 1; cas <= T; cas ++) {
    solve();
  }
}